<?php
/**
 * Date: 2017/11/19
 * Time: 21:13
 * Author: zeronlee
 */
namespace App\Http\Controllers\Traits;

use App\Models\Access\User\User;
use App\Models\Ogz;
use App\Models\App;
use Illuminate\Http\Request;
use App\Models\Access\Role\Role;

trait Base{
    //接口token
    public function getToken(){

        return response()->json(['code'=>0,'msg'=>'','content'=>csrf_token()]);
    }
    //按钮开关接口
    public function getNode(Request $request){
        $mode = $request->mode;
        //按钮命名规则：当前类名去掉controller,在连接字符串‘-’和$act,最终全小写
        $data['code'] = 0;
        //后期模块过多，采取在每个页面请求传入mode
        if (empty($mode))
                $mode = ['user','role','permission','ogz','qyapp','vuls','cate','level','log','addition','goods','goodscate','order','announce','distribute','chartsanalysis','projectmanage','checkoption','sysconfig','knowledge'];
        elseif(is_string($mode))
            $mode = [$mode];
        //增加、删除（禁用）、更新、详情、审核、确认、导出、恢复、物理删除、重判积分等级, 重新编辑等级积分
        $act = ['add','delete','update','checkForm','ensureForm','repairForm','confirmForm','appealForm','shutDownForm','solvedForm','ignoreForm','noEffect','detail','autio','confirm','export','restore','forcedel','sentencepoints','vulsreedit','distribute','delay','auditdelay','projectvuls','index','set','send'];
        $cm = count($mode);
        $ca = count($act);

        header('Content-type: text/css');
        $css = '';
        for ($i=0; $i<$cm; $i++){

            for ($j=0; $j<$ca; $j++){
                $access = access()->allow($mode[$i] .'-'.$act[$j]);
                if (!$access){
                    $css .= '.' . $mode[$i] .'-'.$act[$j] . '{display:none;}';
                }
            }
        }
        $css .= '';
        exit($css);
    }
    //用户所属机构及关联应用
    public function userLinks () {

        $userLinks['ogzs'] = ['parent' => [-1], 'child' => [-1]];
        //拥有机构权
        if (access()->hasOgzs()){
            $userLinks['ogzs']['child'] = access()->hasOgzs();
        }
        //没有的机构权,tree显示使用
        if ($noOgzs = access()->noOgzs()){

            $poc = collect($noOgzs)->flatten()->unique()->toArray();
            $userLinks['ogzs']['parent'] = $poc;

        }
        //拥有应用权限
        $userLinks['apps'] = [-1];
        if (access()->hasApps()){
            $userLinks['apps'] = access()->hasApps();
        }
        //拥有查看漏洞列表的状态
        $userLinks['status'] = [-1];
        if (access()->hasStatus()){
            $userLinks['status'] = access()->hasStatus();
        }
        return $userLinks;
    }

    //加权限选项机构
    public function getOgzs ($fields,$where=[]){
        $userLinks = $this->userLinks();
        $ogzs = Ogz::getList($fields,$where);
        $ogz = new Ogz();
        foreach ($ogzs as $item){
            $item->hasChild = $ogz->hasChild($item->id);
            //角色拥有all属性拥有所有
            if (access()->hasAll()){
                $item->disabled = false;
            } else {
                //设置无权限禁用
                $item->disabled = false;
                $parentOgz = access()->noOgzs();
                if (!in_array($item->id,$userLinks['ogzs']['child'])){
                    $item->disabled = true;
                }
                if (isset($parentOgz[$item->id])){
                    if (in_array($item->id,$parentOgz[$item->id])){
                        $item->disabled = true;
                    }
                }

            }
        }
        return $ogzs;
    }
    //全局检索条件
    public function scopeFilter ($ogzOrApp){
        $r = [];
        $userLinks = $this->userLinks();

        switch ($ogzOrApp){
            case 'ogz':
                $r = $userLinks['ogzs']['child'];
                break;
            case 'app':
                $r = $userLinks['apps'];
                break;
        }
            return $r;
    }
    //用户是否是超级管理员,决定因素是是否拥有all属性
    public function userIsSuperAdmin (){
        foreach (auth()->user()->roles as $role){
            if ($role->all===Role::ACCESS_ALL){
                return true;
            }
        }
        return false;
    }
}